<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
$is_default = false;
$error = array();

//
if( $action == 'getpassword' || $action == 'createnewpassword' ) {
	//valid username/email address
	if( isset($_POST['field_username']) ) {
		$username = trim(util_get_param($_POST, 'field_username', ''));
		$email_address = trim(util_get_param($_POST, 'field_email_address', ''));
		
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.validate.class.php');
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES_VALIDATE . 'account.validate.class.php');
		$vObj = new AccountValidate();
		$vObj->checkEmail($email_address);
		if( $vObj->_valid() ) {
			$user = $userObj->getUser($username, 'username');
			if( $user['users_id'] > 0 ) {
				$password = $userObj->_generate_password();
				$userObj->updatePassword($user['users_id'], $password);
				
				$content = array('user' => $user, 'password' => $password);
				
				//notify
				include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'user.mail.class.php');
				$umnObj = new UserMailNotify();
				$umnObj->setMailTo($user['users_email_address'], $user['users_firstname']);
				$umnObj->addContent($content);
				$umnObj->bindPasswordContent();
				$umnObj->setMailContent(':password_retrieve');
				
				$umnObj->sendmail();
				
				//cron
				include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.cron.class.php');
				$cronObj = new CoreCronBase();
				$cronObj->updateNextRun('mail_queue', 'link');
				
				//
				util_redirect('member/password/success');
			} elseif( $user['users_email_address'] != $email_address ) {
				$vObj->_add('field_email_address', 'The username and email address do not match');
			} else {
				$vObj->_add('field_email_address', 'The user does not exist');
			}
			
			$vm = $vObj->_result(':error');
			$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vm', $vm);
		}
	} else {
		//
		$email_address = trim(util_get_param($_POST, 'field_email_address', ''));
		
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.validate.class.php');
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES_VALIDATE . 'account.validate.class.php');
		$vObj = new AccountValidate();
		$vObj->checkEmail($email_address);
		if( $vObj->_valid() ) {
			$user = $userObj->getUser($email_address, 'email_address');
			if( $user['users_id'] > 0 ) {
				$password = $userObj->_generate_password();
				$userObj->updatePassword($user['users_id'], $password);
				
				$content = array('user' => $user, 'password' => $password);
				
				//notify
				include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'user.mail.class.php');
				$umnObj = new UserMailNotify();
				$umnObj->setMailTo($user['users_email_address'], $user['users_firstname']);
				$umnObj->addContent($content);
				$umnObj->bindPasswordContent();
				$umnObj->setMailContent(':password_retrieve');
				
				$umnObj->sendmail();
				
				//cron
				include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.cron.class.php');
				$cronObj = new CoreCronBase();
				$cronObj->updateNextRun('mail_queue', 'link');
				
				//
				util_redirect('member/password/success');
			} else {
				$vObj->_add('field_email_address', 'The email address does not exist');
			}
			
			$vm = $vObj->_result(':error');
			$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vm', $vm);
		}	
	}
}

//
$bcObj->add(':password', $request_type . '/');

//
if( $request_link_subclass == 'success' ) {
	$content_wrap['mainpart'] = 'success';	
} else {
	$content_wrap['mainpart'] = 'main';
	
	//
	if( $userObj->isLogin() ) {
		$user = $userObj->getCurrentUser();
		$content_wrap['email_address'] = $user['users_email_address'];
	}
}

//
$feObj->content_wrap = $content_wrap;
echo $content_output = $feObj->display('pagelayout/pagelayout_member_password.tpl', $content_wrap);
//
?>